1
2 using
System;
3 using
System.Collections.Generic;
4 using
System.Text;
5 //Needed

6 using
System.Drawing;
7 using
System.Drawing.Drawing2D;
8 using
System.Drawing.Imaging;
9
10 namespace
QuanLyNhanSu
11 {
12     
class BitmapToRegion
13     {
14         
//This method uses only safe code (no pointers) to scan an imagine
15         
//and create a region from it
16         
public static Region getRegion(Bitmap inputBmp, Color transperancyKey, int tolerance)
17         {
18             
//Stores all the rectangles for the region
19             GraphicsPath path =
new GraphicsPath();
20
21             
//Scan the image
22             
for (int x = 0; x < inputBmp.Width; x++)
23             {
24                 
for (int y = 0; y < inputBmp.Height; y++)
25                 {
26                     
if (!colorsMatch(inputBmp.GetPixel(x, y), transperancyKey, tolerance))
27                         path.AddRectangle(
new Rectangle(x, y, 1, 1));
28                 }
29             }
30
31             
//Create the Region
32             Region outputRegion =
new Region(path);
33
34             
//Clean up
35             path.Dispose();
36
37             
//Finish
38             
return outputRegion;
39         }
40
41         
private static bool colorsMatch(Color color1, Color color2, int tolerance)
42         {
43             
if (tolerance < 0) tolerance = 0;
44             
return Math.Abs(color1.R - color2.R) <= tolerance &&
45                    Math.Abs(color1.G - color2.G) <= tolerance &&
46                    Math.Abs(color1.B - color2.B) <= tolerance;
47         }
48
49         
private unsafe static bool colorsMatch(uint* pixelPtr, Color color1, int tolerance)
50         {
51             
if (tolerance < 0) tolerance = 0;
52
53             
//Convert the pixel pointer into a color
54             
byte a = (byte)(*pixelPtr >> 24);
55             
byte r = (byte)(*pixelPtr >> 16);
56             
byte g = (byte)(*pixelPtr >> 8);
57             
byte b = (byte)(*pixelPtr >> 0);
58             Color pointer = Color.FromArgb(a, r, g, b);
59
60             
//Each value between the two colors cannot differ more than tolerance
61             
return Math.Abs(color1.A - pointer.A) <= tolerance &&
62                    Math.Abs(color1.R - pointer.R) <= tolerance &&
63                    Math.Abs(color1.G - pointer.G) <= tolerance &&
64                    Math.Abs(color1.B - pointer.B) <= tolerance;
65         }
66
67         
//Uses pointers to scan through the bitmap a LOT faster
68         
//Make sure to check "Allow unsafe code" in the project properties
69         
public unsafe static Region getRegionFast(Bitmap bitmap, Color transparencyKey, int tolerance)
70         {
71             
//Bounds
72             GraphicsUnit unit = GraphicsUnit.Pixel;
73             RectangleF boundsF = bitmap.GetBounds(
ref unit);
74             Rectangle bounds =
new Rectangle((int)boundsF.Left, (int)boundsF.Top,
75                                              (
int)boundsF.Width, (int)boundsF.Height);
76
77             
int yMax = (int)boundsF.Height;
78             
int xMax = (int)boundsF.Width;
79
80             
//Transparency Color
81             
if (tolerance <= 0) tolerance = 1;
82
83
84             
//Lock Image
85             BitmapData bitmapData = bitmap.LockBits(bounds, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
86             
uint* pixelPtr = (uint*)bitmapData.Scan0.ToPointer();
87
88             
//Stores all the rectangles for the region
89             GraphicsPath path =
new GraphicsPath();
90
91             
//Scan the image, looking for lines that are NOT the transperancy color
92             
for (int y = 0; y < yMax; y++)
93             {
94                 
byte* basePos = (byte*)pixelPtr;
95
96                 
for (int x = 0; x < xMax; x++, pixelPtr++)
97                 {
98                     
//Go on with the loop if its transperancy color
99
100                     
if (colorsMatch(pixelPtr, transparencyKey, tolerance))
101                         
continue;
102
103                     
//Line start
104                     
int x0 = x;
105
106                     
//Find the next transparency colored pixel
107                     
while (x < xMax && !colorsMatch(pixelPtr, transparencyKey, tolerance))
108                     {
109                         x++;
110                         pixelPtr++;
111                     }
112
113                     
//Add the line as a rectangle
114                     path.AddRectangle(
new Rectangle(x0, y, x - x0, 1));
115                 }
116
117                 
//Go to next line
118                 pixelPtr = (
uint*)(basePos + bitmapData.Stride);
119             }
120
121             
//Create the Region
122             Region outputRegion =
new Region(path);
123
124             
//Clean Up
125             path.Dispose();
126             bitmap.UnlockBits(bitmapData);
127
128             
return outputRegion;
129         }
130     }
131 }



Quản lý nhân sự công ty bằng c# _ full source code 60.448 lượt xem

Gõ tìm kiếm nhanh...